package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocom.common.entity.card.EntityXfShoppingCart;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payhandle.ShoppingCartRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfShoppingCart;
import com.ocom.payhandle.service.XfShoppingCartService;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class XfShoppingCartServiceImpl extends JoinServiceImpl<IDaoXfShoppingCart, EntityXfShoppingCart> implements XfShoppingCartService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<EntityXfShoppingCart> getDishesFormCart(Long companyId, ShoppingCartRequest request) {

        if (request.getList().size() == 0) {
            return new ArrayList<>();
        }

        QueryWrapper<EntityXfShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("com_id","p_id","mealplan_id","merch_id","point_id",
                "meal_date", "meal_no", "dishes_id", "dishes_name", "price_mode",
                "photo_url",  "state",  "plat_order_no", "sum(weight_number) as weight_number","sum(dishes_price) as dishes_price ");
        queryWrapper.eq("com_id", request.getCompanyId());
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("state", 0);
        queryWrapper.eq("p_id", request.getPId());
        queryWrapper.and(wrapper -> {
            for (ShoppingCartRequest.ShoppingCartMealListVo shoppingCartMealListVo : request.getList()) {
                wrapper.or(wrapper2 -> wrapper2
                        .eq("meal_no", shoppingCartMealListVo.getMealNo())
                        .eq("meal_date", shoppingCartMealListVo.getMealDate())
                        .eq("point_id", shoppingCartMealListVo.getPointId())
                );
            }
        });

        queryWrapper.groupBy("point_id,meal_date,meal_no");
        queryWrapper.orderByAsc("meal_date,meal_no");
        return this.list(queryWrapper);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateDishesFormCart(Long companyId, ShoppingCartRequest request, Integer state) {
        UpdateWrapper<EntityXfShoppingCart> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("com_id", request.getCompanyId());
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("p_id", request.getPId());

        queryWrapper.and(wrapper -> {
            for (ShoppingCartRequest.ShoppingCartMealListVo shoppingCartMealListVo : request.getList()) {
                wrapper.or(wrapper2 -> wrapper2
                        .eq("meal_no", shoppingCartMealListVo.getMealNo())
                        .eq("meal_date", shoppingCartMealListVo.getMealDate())
                        .eq("point_id", shoppingCartMealListVo.getPointId())
                );
            }
        });

        queryWrapper.set("state",state);
        queryWrapper.set("plat_order_no",request.getTradeNo());
        return this.update(queryWrapper);
    }


}
